<?php

namespace backend\controllers;

use Yii;
use backend\models\PayTrade;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use backend\models\AfterServiceModel;
use backend\models\AfterServiceSearch;
use yii\grid\GridView;
use yii\helpers\Html;
use backend\models\AfterService;
use yii\base\Object;
/**
 * 售后 控制器 test
 * @author suiyue
 *
 */
class AfterServiceController extends BaseController
{
	/**
	 * (non-PHPdoc)行为
	 * @see \yii\base\Component::behaviors()
	 */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	$action_list = array(
    			'index',
    			'update',
    			'view',
    			'create',
    			'upload',
    			'paytrade_view'
    	);
    	 $access=$this->checkAccess($action_list,$action);//判断操作的action是否在actionlist里面
    	 $this->beforeOp('after-service', $access);//身份识别 权限验证  action验证
    	 return true;
    }
	/**
	 * 售后订单列表
	 */
    public function actionIndex()
    {
    	$searchModel = new AfterServiceSearch();
    	$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    	
    	return $this->render('index', [
    			'searchModel' => $searchModel,
    			'dataProvider' => $dataProvider,
    	]);
    }
    /**
     * 创建
     */
    public function actionCreate(){
    	$this->layout = 'main_small';
    	$model = new AfterService();
    	if ($model->load(Yii::$app->request->post())&&$model->validate()) {
    		//先验证订单号是否存在
    		$post=Yii::$app->request->post();
    		$checkTradeno=$model->CheckTradeno($post['AfterService']['trade_no']);
    		// if($checkTradeno){ 临时取消订单号唯一确认
    		 	$model->status = 0;//状态默认为待处理
    		 	
    		 	$post=Yii::$app->request->post();
    		 	$updatetime=date("Y-m-d H:i:s",time());
    		 	if($post['AfterService']['content']!=''){
    		 		$model->content=$post['AfterService']['content']."<br/> By:". Yii::$app->user->identity->username."&nbsp;&nbsp;".$updatetime."<br/>";
    		 	}
    		 	 
    		 	$model->createtime = time();
    		 	$model->updatetime = time();
    		 	$model->create_uid=Yii::$app->user->identity->id;
    		 	$model->save();
    		 	return $this->redirect(['view', 'id' => $model->id]);
    		/*  }else{
    		 	throw new ForbiddenHttpException(Yii::t('app','Sorry , tradeno is not exits!'));
    		 } */
    		
    	} else {
    		return $this->render('create', [
    				'model' => $model,
    		]);
    	}
    }
    
    
    /**
     * 修改
     */
    public function actionUpdate($id){
    	$this->layout = 'main_small';
    	$model = $this->findModel($id);
    	$content=$model->content;
    	$procontent=$model->process_content;
    	if ($model->load(Yii::$app->request->post())&&$model->validate()) {
    		//先验证订单号是否存在
    		$post=Yii::$app->request->post();
    		$checkTradeno=$model->CheckTradeno($post['AfterService']['trade_no']);
    		//if($checkTradeno){ 临时取消订单号唯一确认
	    		$post=Yii::$app->request->post();
	    		$updatetime=date("Y-m-d H:i:s",time());
	    		if($post['AfterService']['insert_content']!=''){
	    			$model->content=$content.$post['AfterService']['insert_content']."<br/>By:". Yii::$app->user->identity->username."&nbsp;&nbsp;".$updatetime."<br/>";
	    		}
	    		if($post['AfterService']['insert_procontent']!=''){
	    			$model->process_content=$procontent.$post['AfterService']['insert_procontent']." <br/>By:". Yii::$app->user->identity->username."&nbsp;&nbsp;".$updatetime."<br/>";
	    		}
	    		
	    	    $model->insert_content="";
	    	    $model->insert_procontent="";
	    		$model->updatetime = time();
	    		$model->update_uid=Yii::$app->user->identity->id;
	    		$model->save();
	    		return $this->redirect(['view', 'id' => $model->id]);
    		/* }else{
    			throw new ForbiddenHttpException(Yii::t('app','Sorry , tradeno is not exits!'));
    		} */	
    	} else {
    		return $this->render('update', [
    				'model' => $model
    		]);
    	}
    }
    
    /**
     * 查看单条数据
     */
    public function actionView($id){
    	$this->layout = 'main_small';
    	$model = AfterService::findOne($id);
    	return $this->render('view', [
    			'model' => $model
    	]);
    }
    /**
     * 显示订单
     * @param unknown $tradeno
     */
    public function actionPaytrade_view($trade_no){
    	$Trademodel=PayTrade::findBySql("select id from pay_trade where trade_no=:trade",[":trade"=>$trade_no])->asArray()->one();
    	if(count($Trademodel)>0){
    		$tradeid=intval($Trademodel['id']);
    		if($tradeid>0){
    			return $this->redirect(array('/pay-trade/view',"id"=>$tradeid));
    		}else{
    			return $this->redirect(array('/pay-trade/index'));
    		}
    	}else{
    		//$this->redirect(array('user/getuser','id'=>1));  
    		return $this->redirect(array('/pay-trade/index'));
    	}
    }
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = AfterService::findOne($id)) !== null) {
        	$model = AfterService::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * (non-PHPdoc)
     * @see \yii\base\Controller::actions()
     * by suiyue
     */
    public function actions(){
    	return [
    			'upload'=>[
    					'class'=>'backend\widgets\ueditor\UEditorAction',
    					'config'=>[
    							//  /gjj/backend/web/images/
    							'imageUrlPrefix'=>"https://".$_SERVER['SERVER_NAME']."/gjj/backend/web/images/",//图片访问路径前缀
    							'imagePathFormat'=>'{rand:2}/{time}{rand:6}',
    							'imageRoot'=>\Yii::getAlias("images")
    					]
    			]
    	];
    
    }
}
